home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-08-28 | 5.9 KB | 246 lines | [TEXT/CWIE] |
- /*
- File: LColorPicker.cp
-
- Contains: Support for modeless and movable modal color pickers.
-
- ColorPicker 2.0 encapsulated for easier usage in C++.
- - it has some really cool features no one uses,
- probably because Apple made it very flexible and thereby
- complicated the interface immensely.
-
- Version: 1.0a1
-
- Written by: Chris Thomas
-
- Copyright: © 1995 Chris K. Thomas. All Rights Reserved.
-
- Change History (most recent first):
- 8/27/95 ckt Fix RGB typing bugs in ColorSync.
- 8/26/95 ckt Moved menu stuff to new
- framework-specific subclasses.
- 8/25/95 ckt Created
-
- Dependencies:
- Requires C++ exceptions.
- Requires RTTI (CW/7 and later).
- Throws OSErrs.
- Subclass of LSharable if PowerPlant is available
- (see header)
- Doesn't require ColorSync or ColorPicker 2.0.
- Doesn't use ColorSync even if it is present, so that
- ColorPicker Mgr handles conversion to/from RGBColor.
- [ColorSync support is planned for 1.1]
- Can't throw from outside the main thread, because
- Metrowerks doesn't have their exceptions implementation
- together.
-
- Is a subclass of LSharable if used with PowerPlant.
-
- Usage:
- myColorPicker = new LColorPicker("\pTrue colors", object->itsColor,
- true, false);
- ...
- WaitNextEvent(everyEvent, &wneEvent...)
-
- myColorPicker->HandleEvent(wneEvent);
-
- */
-
-
- // ——————• Includes———————————————————————————————————————————————————————————————————————
- #include "LColorPicker.h"
-
- // ——————• Static member vars—————————————————————————————————————————————————————————————
- ColorChangedUPP LColorPicker::sColorChangeUpp; // * for live color change detection
-
- // ——————• Lifetime———————————————————————————————————————————————————————————————————————
- #pragma mark • Lifetime
-
- LColorPicker::LColorPicker(StringPtr inPrompt, RGBColor& inOldColor,
- Boolean isMovable, Boolean isModal)
- {
- Point frontAndCenter = {-1, -1};
-
- // mCurrentColor = inOldColor;
- mPicker = NULL;
-
- OSErr theErr;
- long gestaltValue;
-
- theErr = Gestalt(gestaltColorPickerVersion, &gestaltValue);
-
- // * one oh compatibility
- if(theErr != noErr) // * gestaltColorPickerVersion isn't installed previously
- {
- Boolean ok = ::GetColor(frontAndCenter, inPrompt, &inOldColor, &mCurrentColor);
-
- if(ok)
- {
- UserFinalizedColor();
- UserSaysOK();
- }
- else
- {
- UserSaysCancel();
- }
- // * don't reference self afterwards,
- // because we might be deleted by a subclass ("delete this")
- return;
- }
-
- // * fill in the system dialog info for a 2.0 system-owned color picker dialog
- SystemDialogInfo ourInfo;
-
- if(isModal == true)
- ourInfo.flags += DialogIsModal;
-
- if(isMovable)
- ourInfo.flags += DialogIsMoveable;
-
- ourInfo.flags = CanModifyPalette + CanAnimatePalette + DialogIsMoveable;
-
- ourInfo.pickerType = 0L;
- ourInfo.placeWhere = kDeepestColorScreen;
-
- GetMenuItems(ourInfo.mInfo); // * get menu info from subclass
-
- // * create the picker
-
- theErr = CreateColorDialog(&ourInfo, &mPicker);
- if(theErr != noErr)
- throw theErr;
-
- // * set up the picker's variables
-
- SetPrompt(inPrompt);
- SetColor(inOldColor, kOriginalColor);
- SetColor(inOldColor, kNewColor);
-
- SetPickerVisibility(mPicker, true);
- }
-
- LColorPicker::~LColorPicker()
- {
- if(mPicker)
- DisposeColorPicker(mPicker);
- }
-
- // ——————• Events———————————————————————————————————————————————————————————————————
- #pragma mark • Events
-
- // * returns true if the event is a picker event
- short LColorPicker::HandleEvent(EventRecord *inEvent)
- {
- Boolean outPickerEvent;
- EventData ourEventData;
-
- // * create UniversalProcPtrs if necessary
- if(sColorChangeUpp == NULL)
- {
- sColorChangeUpp = NewColorChangedProc(ColorChangeProc);
-
- if(sColorChangeUpp == NULL)
- throw 'sh*t'; // * boy are we in trouble
- // if we can't alloc that
- }
-
- ourEventData.event = inEvent;
- ourEventData.colorProc = sColorChangeUpp;
- ourEventData.colorProcData = (long) this;
-
- GrafPtr savePort;
-
- GetPort(&mSavePort);
- DoPickerEvent(mPicker, &ourEventData);
-
- if(ourEventData.handled)
- {
- switch(ourEventData.action)
- {
- case kColorChanged:
- UserFinalizedColor();
- break;
- case kOkHit:
- UserSaysOK();
- break;
- case kCancelHit:
- UserSaysCancel();
- break;
- default:
- break;
-
- }
-
- return ourEventData.action;
- }
- else
- return kDidNothing;
-
- /*
- • The Color Picker Manager lies about handling events.
- It changes the cursor, then tells us it didn't handle the event.
- There's no easy way to fix this.
-
- • Also, why do we have to handle the edit menu if we already
- told it the correct items and it knows it's own dialog item
- numbers?
- */
- }
-
- Boolean LColorPicker::CanClose()
- {
- EventData ourEvent;
-
- ourEvent.event = NULL;
- ourEvent.forcast = kDialogAccept;
-
- DoPickerEvent(mPicker, &ourEvent);
- return (!ourEvent.handled);
- }
-
- // ——————• Accessors—————————————————————————————————————————————————shrubbery——————————
- #pragma mark • Accessors
-
- void LColorPicker::SetColor(RGBColor &inColor, ColorType inType)
- {
- if(inType == kNewColor)
- mCurrentColor = inColor;
-
- PMColor ourColor;
-
- // * CMRGBColor is the same exact structure as QuickDraw RGBColor! Jeeeez...
- CMRGBColor *ourCMColor = reinterpret_cast< CMRGBColor *>(&inColor);
-
- ourColor.color.rgb = *ourCMColor;
- ourColor.profile = NULL;
-
- SetPickerColor(mPicker, inType, &ourColor);
- }
-
- void LColorPicker::GetColor(RGBColor &outColor)
- {
- outColor = mCurrentColor;
- }
-
- void LColorPicker::SetPrompt(const StringPtr inPrompt)
- {
- SetPickerPrompt(mPicker, inPrompt);
- }
-
- // ——————• Callbacks——————————————————————————————————————————————————————————————————
- #pragma mark • Callbacks
-
- // * handle live color changes
- pascal void LColorPicker::ColorChangeProc(LColorPicker *inInstance, PMColor *inNewColor)
- {
- inInstance->mCurrentColor = *reinterpret_cast<RGBColor *>(&inNewColor->color.rgb);
-
- GrafPtr localSave;
-
- GetPort(&localSave); // * save color picker’s port
- SetPort(inInstance->mSavePort); // * set port back to user’s port
-
- inInstance->UserChangingColor();
-
- SetPort(localSave);
- }